<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\WebPage;

/**
 * Controller for displaying
 * a page with details about
 * one API client app that belongs
 * to Viewer.
 * The data on this page is non-editable
 * but there is a link to "Edit"
 * page at the bottom
 *
 */
class Viewapp extends Editapp
{

	/**
	 * app_id is a required Request param
	 *
	 * @var array
	 */
	protected $aRequired = array('app_id');


	protected function main(){

		$this->setApi()
		->setTitle()
		->setBody();
	}


	protected function setTitle(){
		$this->title = $this->aPageVars['title'] = 'Application details';

		return $this;
	}


	/**
	 * Setup the 'body' of $this->aPageVars
	 *
	 *
	 */
	protected function setBody(){

		$this->aPageVars['body'] = \tplViewapp::parse(array($this->title, $this->getAppDetails(), $this->oApi['_id']), false);

		return $this;
	}


	/**
	 * Get basic details about the app
	 * and prepare the html block with this info
	 * This block is shown on a page after the APP
	 * is added or edited
	 * It contains the data returned by the Clientdata class
	 * In case of OAuth2 - the Clientdata class returns more data,
	 * like urls for Authorization end-points, as well as the
	 * secret token(s)
	 *
	 * @return string html string
	 */
	protected function getAppDetails(){
		$tpl = '<h2 class="definition rounded5">%s</h2><p>%s</p>';
		$ret = '';
		$a = $this->oApi->getData();
		foreach($a as $key => $val){
			$ret .= sprintf($tpl, $key, $val);
		}

		return $ret;
	}

}
